﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     ActivationCode业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/11/5
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class ActivationCodeBLL
    {
        private static ActivationCodeBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IActivationCodeDao dao = null;

        private ActivationCodeBLL()
        {
            dao = ActivationCodeSqlMapDao.Instance;
        }

        public static ActivationCodeBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ActivationCodeBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<ActivationCode> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<ActivationCode> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<ActivationCode> GetListByParameters(object TopCounts, ActivationCode activationcode)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<ActivationCode> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public ActivationCode GetDetail(System.String acid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(acid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过用户ID得到明细
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public ActivationCode GetDetailByUserID(string userID)
        {
            try
            {
                ///daoManager.BeginTransaction();

                ActivationCode code = new ActivationCode();
                code.UserID = userID;

                return dao.GetDetail(code);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="activationcode"></param>
        /// <returns></returns>
        public void Add(ActivationCode code)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try
            {
                ///daoManager.BeginTransaction();
                ActivationCode existedcode = GetCodeByUserIDAndStatus(code.UserID, code.Status);
                if (existedcode == null)
                {
                    dao.Add(code);
                }
                else
                {
                    existedcode.Code = code.Code;
                    Update(existedcode);
                }
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="activationcode"></param>
        /// <returns></returns>
        public int Update(ActivationCode activationcode)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(activationcode);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public int Delete(System.String acid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(acid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="activationcode"></param>
        /// <returns></returns>
        public int Delete(ActivationCode activationcode)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(activationcode);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 根据用户编号和激活码类型获取激活码
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ActivationCode GetCodeByUserIDAndStatus(string userid,int status)
        {
            try
            {
                ActivationCode code = new ActivationCode();
                code.UserID = userid;
                code.Status = status;
                return dao.GetDetail(code);
            }
            catch 
            {
                throw;
            }
        }
    }
}
